package com.levelup.cr.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.levelup.cr.client.rpc.RepositoryRemoteService;
import com.levelup.cr.client.rpc.RepositoryRemoteServiceAsync;
import com.levelup.cr.model.dto.NodeTransfer;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 9/24/13
 * Time: 7:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class RepositoryWidget extends VerticalPanel{

    final RepositoryRemoteServiceAsync repositoryService = GWT.create(RepositoryRemoteService.class);
    final TextBox nameText = new TextBox();
    final TextBox pathText = new TextBox();
    final ListBox propertiesBox =  new ListBox(true);

    public RepositoryWidget() {
        init();
    }

    private void init(){
        HorizontalPanel namePanel = new HorizontalPanel();
        HorizontalPanel pathPanel = new HorizontalPanel();
        HorizontalPanel propertiesPanel = new HorizontalPanel();

        namePanel.setSize("500","120");
        pathPanel.setSize("500","120");
        propertiesPanel.setSize("500","400");

        Label nameLabel = new Label("Node name:");

        nameText.setSize("200","80");

        namePanel.add(nameLabel);
        namePanel.add(nameText);

        Label pathLabel = new Label("Node path:");

        pathText.setSize("200","80");

        pathPanel.add(pathLabel);
        pathPanel.add(pathText);

        Label propertiesLabel = new Label("Properties:");

        propertiesBox.setSize("350","380");

        propertiesPanel.add(propertiesLabel);
        propertiesPanel.add(propertiesBox);

        getDefaultNode();

        this.add(namePanel);
        this.add(pathPanel);
        this.add(propertiesPanel);
    }

    private void getDefaultNode() {
        String path = "/level1";
        repositoryService.getNodeByPath(path,-1,new AsyncCallback<NodeTransfer>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error");
            }

            @Override
            public void onSuccess(NodeTransfer result) {
                processNode(result);
            }
        });
    }

    private void processNode(NodeTransfer node){
        nameText.setValue(node.getName());
        String parentName = "";
        if (node.getParent()!=null){
            parentName = node.getParent().getName();

        }
        pathText.setValue(parentName+"/"+node.getName());
        propertiesBox.clear();
        HashMap<String,Object> properties = node.getProperties();
        Iterator<String> iterator = properties.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = (String) properties.get(key);
            propertiesBox.addItem(key+":"+value);

        }
    }
}
